package com.edu.security.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.edu.security.entity.User;
import com.edu.security.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.provisioning.UserDetailsManager;

import java.util.ArrayList;
import java.util.Collection;

// 创建基于数据库的用户信息管理
@Configuration
//public class DBUserDetailManager implements UserDetailsManager, UserDetailsPasswordService {
public class DBUserDetailManager implements UserDetailsService, UserDetailsManager {
    @Autowired
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.getOne(
                new QueryWrapper<User>()
                        .eq("username", username));
        if (user == null) {
            throw new UsernameNotFoundException(username);
        } else {
            Collection<GrantedAuthority> authorities = new ArrayList<>();
            if (user.getUsername().equals("Tom"))
                authorities.add((GrantedAuthority) () -> "USER_LIST");
            else if (user.getUsername().equals("rain"))
                return org.springframework.security.core.userdetails.User
                        .withUsername(user.getUsername())
                        .password(user.getPassword())
                        .disabled(!user.getEnabled())
                        .credentialsExpired(false)
                        .accountLocked(false)
                        .roles("ADMIN")
                        .build();
            return new org.springframework.security.core.userdetails.User(
                    user.getUsername(),
                    user.getPassword(),
                    user.getEnabled(),
                    true, // 用户账号是否过期
                    true, // 用户凭证是否过期
                    true, // 用户是否未被锁定
                    authorities // 权限列表
            );
        }
    }

    @Override
    public void createUser(UserDetails user) {
        User user1 = new User(user.getUsername(), user.getPassword());
        User one = userService.getOne(new QueryWrapper<User>().eq("username", user.getUsername()));
        if (one == null) {
            userService.save(user1);
        }

    }

    @Override
    public void updateUser(UserDetails user) {

    }

    @Override
    public void deleteUser(String username) {

    }

    @Override
    public void changePassword(String oldPassword, String newPassword) {

    }

    @Override
    public boolean userExists(String username) {
        return false;
    }
}
